<?php
protectScript('UserModel.php');

class UserModel{
//	private $_asmReadOnlyProps = array(
//		'sTableName' => 'account',
//		'sTablePrefix' => TABLE_PREFIX
//	);
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'user',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdUser' => 0,
		'sNameUser' => '',
		'sPassword' => '',
		'sEmailUser' => '',
		'iRankUser' => -1
	);

	public function __construct($asmUserInfo = null){
		if($asmUserInfo !== null){
			$this->_asmWritableProps['iIdUser'] = $asmUserInfo['idUser'];
			$this->_asmWritableProps['sNameUser'] = $asmUserInfo['nameUser'];
			$this->_asmWritableProps['sPassword'] = $asmUserInfo['passwordUser'];
			$this->_asmWritableProps['sEmailUser'] = $asmUserInfo['emailUser'];
			$this->_asmWritableProps['iRankUser'] = $asmUserInfo['rankUser'];
		}
	}

	public static function checkName($sNameToCheck){
		$sPattern = "#^[A-Za-zéè]{3,}[ ]?[A-Za-zéè]{3,}$#";

		if (preg_match($sPattern, $sNameToCheck) AND strlen($sNameToCheck) <= 25){
			return 1;
		}else{
			throw new InvalidNameException(
				'Your name isn\'t valid. Please choose one different.',
				ERROR,
				array(
					'scriptName' => 'UserModel.php',
					'nameToCheck' => $sNameToCheck
				)
			);
		}
	}

	public static function checkPass($sPassToCheck, $sName){
		$oMysqliInstance = AYSMysqli::getInstance();

		$query = sprintf(
			'SELECT idUser ' .
			"FROM %s " .
			"WHERE passwordUser = '%s' AND nameUser = '%s'",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			sha1($oMysqliInstance->escape_string($sPassToCheck)),
			$oMysqliInstance->escape_string($sName)
		);

		$rResultSelect = $oMysqliInstance->query($query);
		if($rResultSelect->num_rows !== 1){
			throw new InvalidPassException(
				'Le mot de passe n\'est pas correct.',
				INFO,
				array(
					'DataEntered' => array(
						$sPassToCheck, $sName
					)
				)
			);
		}else{
			return 1;
		}
	}

	public static function getInfosFromName($sName){
		$oMysqliInstance = AYSMysqli::getInstance();

		$query = sprintf(
			'SELECT idUser, emailUser, rankUser ' .
			"FROM %s " .
			"WHERE nameUser = '%s'",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($sName)
		);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'UserModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
